﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using stockUploader.Core;

namespace stockUploader
{
    class Program
    {
        private static SlotMachine _slotMachine = new SlotMachine(10);
        private static bool _processing = false;
        private static Settings _stocks = Settings.Load();

        static void Main(string[] args)
        {
            try
            {
                DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
                FileInfo[] jpegs = di.GetFiles("*.jpg");

                foreach(var stock in _stocks.Stocks)
                {
                    Console.WriteLine();
                    Console.WriteLine(stock.Ftp);
                    foreach(FileInfo file in jpegs)
                    {
                        initUpload(file, stock);
                    }
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine();
                Console.WriteLine("Error: " + ex.Message);
                Console.WriteLine("Source: " + ex.Source);
                Console.WriteLine("Stack: " + ex.StackTrace);
            }
            // loop main thread
            //while(!_slotMachine.IsAllFree() || _processing) ; 

            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
        }

        private static void initUpload(FileInfo file, Stock stock)
        {
            doUpload(file, stock);
        }

        private static void doUpload(FileInfo file, Stock stock)
        {
            //while(!_slotMachine.HasFreeSlot()) ;
            lock(_slotMachine)
            {
                //int slot = _slotMachine.GetFreeSlot();
                FtpStockClient fc = new FtpStockClient(stock);
                fc.UploadFile(file);

                //Token token = new Token
                //{
                //    Slot = slot,
                //    Request = fc.Request,
                //    File = file,
                //    Stock = stock
                //};


                //IAsyncResult result = fc.Request.BeginGetResponse(new AsyncCallback(requestCallback), token);
            }
        }

        //public static void requestCallback(IAsyncResult result)
        //{
        //    Token token = (Token)result.AsyncState;
        //    if(result.IsCompleted)
        //    {
        //        _processing = true;
        //        lock(_slotMachine)
        //        {
        //            _slotMachine.FreeSlot(token.Slot);
        //        }

        //        try
        //        {
        //            FtpWebResponse response = (FtpWebResponse)token.Request.EndGetResponse(result);
        //            processResponse(response, token);
        //        }
        //        catch(WebException webex)
        //        {
        //            if(webex.Response is FtpWebResponse)
        //            {
        //                processResponse(webex.Response as FtpWebResponse, token);
        //            }
        //        }
        //        catch(Exception ex) { }
        //        _processing = false;
        //    }
        //}

        //private static void processResponse(FtpWebResponse response, Token token)
        //{
        //    if(response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable
        //        && token.Method == WebRequestMethods.Ftp.GetFileSize)
        //    {
        //        newFileUpload(token);
        //    }
        //}

        //private static void newFileUpload(Token token)
        //{
        //    doUpload(token.File, token.Stock, WebRequestMethods.Ftp.UploadFile);
        //}
    }
}
